from fastapi import Depends
from api.src.db.database import get_db
from sqlalchemy.orm import Session
from sqlalchemy import and_
from sqlalchemy.exc import PendingRollbackError

from lib import utils

from . import models, schemas


############################# Users ###################################
def get_user(user_id: int, db: Session = next(get_db())):
    detail = db.query(models.User).filter_by(id=user_id).first()
    db.close()
    return detail


def get_user_by_open_id(open_id: str, db: Session = next(get_db())):
    detail = (
        db.query(models.User).filter(and_(models.User.wx_open_id == open_id)).first()
    )
    db.close()
    return detail


def get_users(skip: int = 0, limit: int = 100, db: Session = next(get_db())):
    list = db.query(models.User).offset(skip).limit(limit).all()
    db.close()
    return list


def update_user_invite_code(
    user_id: int, invite_code: str, db: Session = next(get_db())
):
    print(invite_code)
    db.query(models.User).filter(models.User.id == user_id).update(
        {"invite_code": invite_code}
    )
    db.commit()
    db.close()
    return invite_code


def create_user(
    user: schemas.UserCreate,
    interest: schemas.InterestCreate,
    invitation: schemas.InvitationCreate = None,
    db: Session = next(get_db()),
):
    try:
        db_user = models.User(**user.model_dump())
        db.add(db_user)
        db.flush()
        # 写入权益
        db_interest = models.Interest(**interest.model_dump())
        db_interest.user_id = db_user.id
        db.add(db_interest)
        # 写入邀请关系
        if invitation is not None:
            invitation.invitee_id = db_user.id
            db_invitation = models.Invitation(**invitation.model_dump())
            db.add(db_invitation)
        db.flush()
        db.commit()
    except PendingRollbackError:
        db.rollback()
    finally:
        db.close()
    return db_user


def get_interest(user_id: int, db: Session = next(get_db())):
    detail = (
        db.query(models.Interest)
        .filter(and_(models.Interest.user_id == user_id))
        .first()
    )
    db.close()
    return detail


############################## Orders ##################################
def get_order_list(
    user_id: int, page: int = 0, limit: int = 10, db: Session = next(get_db())
):
    skip = (page - 1) * limit
    list = (
        db.query(models.Order)
        .filter_by(user_id=user_id)
        .order_by(models.Order.id.desc())
        .offset(skip)
        .limit(limit)
        .all()
    )
    db.close()
    return list


def create_order(order: schemas.OrderCreate, db: Session = next(get_db())):
    db_order = models.Order(**order.model_dump())
    db.add(db_order)
    db.commit()
    db.refresh(db_order)
    db.close()
    return db_order


def get_order(user_id: int, order_id: int, db: Session = next(get_db())):
    detail = (
        db.query(models.Order)
        .filter(and_(models.Order.user_id == user_id, models.Order.id == order_id))
        .first()
    )
    db.close()
    return detail


################################# Invitations #######################################


def get_invitations(
    inviter_id: int, page: int, page_size: int, db: Session = next(get_db())
):
    skip = (page - 1) * page_size
    list = (
        db.query(models.Order)
        .filter_by(inviter_id=inviter_id)
        .offset(skip)
        .limit(page_size)
        .all()
    )
    db.close()
    return list
